package ricky.scene
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	
	import ricky.Interface.IDispose;
	import ricky.animate.AnimSprite;
	import ricky.cache.ResourceCahch1;
	import ricky.events.CameraEvent;
	import ricky.game.graphic.Graphic;
	import ricky.loader.MultipleLoader;
	import ricky.loader.Request;

	/**
	 * 游戏中的地图层
	 * @discription 游戏世界中的地图层元素
	 * @author pu
	 * @date 2011.12.5
	 */
	public class MapLayout extends Sprite implements IDispose
	{
		public function MapLayout()
		{
			this._mapLoadDir=new Dictionary();
			this.mouseChildren=false;
			this.mouseEnabled=false;
		}
		
		//************************** public (***********************************
		
		/** 是否初始化 */
		public function initialized():Boolean
		{
			return _initialized;
		}

		/**
		 * 清理场景地毯层的所有数据
		 */
		public function clear():void
		{
			_mapLoadDir=new Dictionary();
			var obj:*;
			while(this.numChildren>0)
			{
				obj=this.removeChildAt(0);
				if(obj is IDispose)
					obj.dispose();
			}
		}
		
		/**
		 * 改变大小，stage3d版本才需要调用
		 */
		public function resize(width:Number,height:Number,x:Number=NaN,y:Number=NaN):void
		{
			
		}
		
		/**
		 * 跟随镜头移动，stage3d版本才需要调用
		 */
		public function moveing():void
		{
			
		}
		
		/**
		 * 设置地图格子信息用来创建地图数据
		 */
		public function setGridInfo(gridW:Number,gridH:Number):void
		{
			this._gridWidth=gridW;
			this._gridHeight=gridH;
		}
		//**************************************** private **********************************
				
		/**
		 *绘制缓冲区
		 */
		public function loadResource(gx:int,gy:int,gwidth:int,gheight:int):void
		{

			var vtsx:int=gx+gwidth;
			var vtsy:int=gy+gheight;
			for(var i:int=gx; i<=vtsx;i++)
			{
				for(var j:int=gy;j<=vtsy;j++)
				{
//					trace(i+","+j);
					if(!_mapLoadDir[i+","+j])
						loadTile(i,j);
				}
			}	
		}
		
		/**
		 * 加载完成
		 */
		protected function onComplete(e:Request):void
		{
			var i:int=0,j:int=0;
			var s:Array=e.head.split(",");
			i=int(s[0]);
			j=int(s[1]);
			
			var x1:Number=i*gridWidth;
			var y1:Number=j*gridHeight;
			var bmp:Bitmap=e.data;

			if(!_mapLoadDir[e.key])
				_mapLoadDir[e.key]=bmp;

			var bitMap:Bitmap=_mapLoadDir[e.key];

			bitMap.x=x1;
			bitMap.y=y1;
				
			this.addChild(bitMap);
		}
		
		/**
		 * 加载制定坐标格子的资源块
		 */
		protected function loadTile(i:int,j:int):void
		{
			var r:Request=new Request(getFilePath(i,j),onComplete);
			r.key=i+","+j;
			r.head=i+","+j;
			MultipleLoader.instance.loadReq([r],null,null);
		}
		
		/**
		 * 根据坐标得到文件名
		 */
		protected function getFilePath(i:int,j:int):String
		{
			if(_tilePathFun!=null)
				return _tilePathFun(i,j);
			else
				return i+""+j+".jpg";//_mapData.mapURLPath+"\\"+(int(j*gridMaxX)+i+1)+".jpg";
		}
		
		//**************** 接口实现
		
		public function dispose():void
		{
			_mapLoadDir=null;
			
			var obj:*;
			while(this.numChildren>0)
			{
				obj=this.removeChildAt(0);
				if(obj is IDispose)
					obj.dispose();
			}
		}

		//**************** 属性*****************************************

		protected var _gridWidth:Number,_gridHeight:Number;
		protected var _tilePathFun:Function;
		
		public function get gridWidth():Number
		{
			return _gridWidth;
		}

//		public function set gridWidth(value:Number):void
//		{
//			_gridWidth = value;
//		}

		public function get gridHeight():Number
		{
			return _gridHeight;
		}

//		public function set gridHeight(value:Number):void
//		{
//			_gridHeight = value;
//		}

		/**
		 * 地图快资源获取函数
		 * function(i:int,j:int):String return 资源块的路径
		 */
		public function set tilePathFun(value:Function):void
		{
			_tilePathFun = value;
		}

		
		protected var _mapLoadDir:Dictionary;
		
		protected var _initialized:Boolean=false;//是否初始化

	}
}